package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.Comment;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.utils.Constant;

public class CommentDAO extends BaseDAO<Comment>{

	public void addComment(Comment comment) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_user_comment(");
		sb.append("commentTime,userId,commentContent,moduleType,stars,moduleId,parentId");
		sb.append(") values (");
		sb.append(":commentTime,:userId,:commentContent,:moduleType,:stars,:moduleId,:parentId");
		sb.append(")");
		
		update(sb.toString(), comment);
	}
	
	public long addCommentWithResult(Comment comment) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_user_comment(");
		sb.append("commentTime,userId,commentContent,moduleType,stars,moduleId,parentId");
		sb.append(") values (");
		sb.append(":commentTime,:userId,:commentContent,:moduleType,:stars,:moduleId,:parentId");
		sb.append(")");
		
		update(sb.toString(), comment);
		
		return getIncreamentId();
	}
	
	public void updateComment(Comment comment) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("update him_user_comment set ");
		sb.append(" commentTime=:commentTime,userId=:userId,commentContent=:commentContent,");
		sb.append(" moduleType=:moduleType,stars=:stars,moduleId=:moduleId,parentId=:parentId");
		sb.append(" where commentId = :commentId");
		
		update(sb.toString(), comment);
	}
	
	public List<Comment> listAllComments() throws Exception{
		String sql = "select * from him_user_comment";
		return getList(sql, Comment.class);
	}
	
	public List<Comment> listAllComments(int moduleType) throws Exception{
		String sql = "select * from him_user_comment where moduleType = ?";
		return getList(sql, Comment.class,moduleType);
	}
	
	public PageModel getPagedComments(PageModel model,int moduleType,long moduleId,long userId) throws Exception{
		String pre = " select * from (";
		String pre_count = "select count(*) from (";
		String end = " ) as c";
		StringBuilder str = new StringBuilder(" select * from him_user_comment where parentId = 0");
		List<Object> paramList = new ArrayList<Object>();
		
		if(moduleType > 0){
			str.append(" and moduleType = ?");
			paramList.add(moduleType);
		}
		
		if(moduleId > 0){
			str.append(" and moduleId = ?");
			paramList.add(moduleId);
		}
		
		if(userId > 0){
			str.append(" and userId = ?");
			paramList.add(userId);
		}
		
		StringBuilder querySQL = new StringBuilder(pre+str.toString()+end);
		StringBuilder countSQL = new StringBuilder(pre_count+str.toString()+end);
		
		StringBuilder whereSQL = new StringBuilder(" where 1=1 ");
		
		
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, Comment.class);
	}
	
	public PageModel getPagedComments(PageModel model,int moduleType,String title,String username) throws Exception{
		String pre = " select * from (";
		String pre_count = "select count(*) from (";
		String end = " ) as c";
		String sql = "select a.*,b.title title,c.username username from him_user_comment a,him_live_address b,him_user c where a.userId = c.userId and a.moduleId = b.infoId";
		switch(moduleType){
			case Constant.COMM_ADDRESS_INFO_MODULE:
				sql = "select a.*,b.title title,c.username username from him_user_comment a,him_live_address b,him_user c where a.userId = c.userId and a.moduleId = b.infoId";
				break;
			case Constant.COMM_COUPON_INFO_MODULE:
				sql = "select a.*,b.`name` title,c.username username from him_user_comment a,him_coupon_coupon b,him_user c where a.userId = c.userId and a.moduleId = b.couponId";
				break;
			case Constant.COMM_RENT_INFO_MODULE:
				sql = "select a.*,b.title title,c.username username from him_user_comment a,him_func_rent b,him_user c where a.userId = c.userId and a.moduleId = b.rentId";
				break;
			case Constant.COMM_SECONDARY_INFO_MODULE:
				sql = "select a.*,b.title title,c.username username from him_user_comment a,him_func_secondary b,him_user c where a.userId = c.userId and a.moduleId = b.infoId ";
				break;
			case Constant.COMM_SIMPLE_INFO_MODULE:
				sql = "select a.*,b.`name` title,c.username username from him_user_comment a,him_live_common b,him_user c where a.userId = c.userId and a.moduleId = b.infoId ";
				break;
			case Constant.COMM_TICKET_INFO_MODULE:
				sql = "select a.*,b.foodName title,c.username username from him_user_comment a,him_book_food b,him_user c where a.userId = c.userId and a.moduleId = b.foodId";
				break;
			default:
				sql = "select a.*,b.foodName title,c.username username from him_user_comment a,him_book_food b,him_user c where a.userId = c.userId and a.moduleId = b.foodId";
				break;
		}
		
		StringBuilder str = new StringBuilder(sql);
		List<Object> paramList = new ArrayList<Object>();
		
		if(moduleType > 0){
			str.append(" and moduleType = ?");
			paramList.add(moduleType);
		}
		
		
		StringBuilder querySQL = new StringBuilder(pre+str.toString()+end);
		StringBuilder countSQL = new StringBuilder(pre_count+str.toString()+end);
		
		StringBuilder whereSQL = new StringBuilder(" where title like ? and username like ? order by commentTime desc");
		paramList.add("%"+title+"%");
		paramList.add("%"+username+"%");
		
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, Comment.class);
	}
	
	public Comment getById(long commentId) throws Exception{
		String sql = "select * from him_user_comment where commentId = ?";
		return this.getJavaBean(sql, Comment.class, commentId);
	}
	
	public void delComment(int commentId) throws Exception{
		String sql = "delete from him_user_comment where commentId = ?";
		this.commonUpdate(sql, commentId);
	}
	
	public long getCountByModule(int moduleType,long moduleId) throws Exception {
		String sql = "select count(*) from him_user_comment where moduleType = ? and moduleId = ?";
		return getCount(sql, moduleType,moduleId);
	}
}
